package cn.com.pansky.service;

import java.io.UnsupportedEncodingException;

import cn.com.pansky.util.PropertiesUtil;

import com.abc.pay.client.Base64;
import com.abc.pay.client.JSON;
import com.abc.pay.client.ebus.QueryOrderRequest;
import com.alibaba.fastjson.JSONObject;

public class ABCQuery {

	@SuppressWarnings({ "unchecked"})
	public JSONObject query(String orderNumber, String AAE051) {
		JSONObject jsonObject = new JSONObject();
		//1、生成交易查询对象
		String payTypeID = "ImmediatePay";
		String queryTpye = "1";
		if(queryTpye.equals("0")){
			queryTpye = "false";
		}else if (queryTpye.equals("1")){
			queryTpye="true";
		}
		QueryOrderRequest tQueryRequest = new QueryOrderRequest();
		tQueryRequest.queryRequest.put("PayTypeID", payTypeID);    //设定交易类型
		tQueryRequest.queryRequest.put("OrderNo", orderNumber);    //设定订单编号 （必要信息）
		tQueryRequest.queryRequest.put("QueryDetail", queryTpye);//设定查询方式
		
		PropertiesUtil proUtil = new PropertiesUtil();
		// 根据商户号获取到所在属性文件中的索引
		int aae051Index = proUtil.getabcAAE051(AAE051);
		JSON json = tQueryRequest.extendPostRequest(aae051Index);
		
//		JSON json = tQueryRequest.postRequest();
		String ReturnCode = json.GetKeyValue("ReturnCode");
		String ErrorMessage = json.GetKeyValue("ErrorMessage");
		System.out.println("农行单笔查询结果======="+orderNumber+"====="+ReturnCode+"=========="+ErrorMessage+"==============="+json.toString());
		if (ReturnCode.equals("0000"))
		{
		    System.out.println("ReturnCode   = [" + ReturnCode + "]<br/>");
		    System.out.println("ErrorMessage = [" + ErrorMessage + "]<br/>");
		    //4、获取结果信息
		    String orderInfo = json.GetKeyValue("Order");
		    if (orderInfo.length() < 1){
		        System.out.println("查询结果为空<br/>");
		    } else{
		        //1、还原经过base64编码的信息 
		    	Base64 tBase64 = new Base64();
		  		String orderDetail = new String(tBase64.decode(orderInfo));
		        json.setJsonString(orderDetail);
		        System.out.println("订单明细" + orderDetail + "<br/>");
		        
//		        json.put("orderAmount", e.elementText("orderAmount"));
//				json.put("orderNumber", e.elementText("orderNumber"));
//				json.put("qid", e.elementText("qid"));
//				json.put("state", e.elementText("state"));
		        
		        jsonObject.put("orderAmount", json.GetKeyValue("OrderAmount"));
		        jsonObject.put("orderNumber", json.GetKeyValue("OrderNo"));
		        jsonObject.put("qid", json.GetKeyValue("iRspRef"));
		        jsonObject.put("state", json.GetKeyValue("Status").equals("04") ? "1" : "0"); //04 代表支付成功
		        jsonObject.put("ReceiveAccount", json.GetKeyValue("ReceiveAccount"));
		        jsonObject.put("AAE010", json.GetKeyValue("AcctNo")); //当事人缴费账户
		        jsonObject.put("MerchantRemarks", json.GetKeyValue("MerchantRemarks"));
		        jsonObject.put("ReceiveAccName", json.GetKeyValue("ReceiveAccName"));
		        jsonObject.put("bankType", json.GetKeyValue("NH")); //缴费银行类别农行
		        if(queryTpye.equals("0")) {
		            System.out.println("PayTypeID      = [" + json.GetKeyValue("PayTypeID") + "]<br/>");
		            System.out.println("OrderNo      = [" + json.GetKeyValue("OrderNo") + "]<br/>");
		            System.out.println("OrderDate      = [" + json.GetKeyValue("OrderDate") + "]<br/>");
		            System.out.println("OrderTime      = [" + json.GetKeyValue("OrderTime") + "]<br/>");
		            System.out.println("OrderAmount      = [" + json.GetKeyValue("OrderAmount") + "]<br/>");
		            System.out.println("Status      = [" + json.GetKeyValue("Status") + "]<br/>");
		        }else{
		            if (payTypeID.equals("ImmediatePay") || payTypeID.equals("PreAuthPay")){
		                System.out.println("PayTypeID      = [" + json.GetKeyValue("PayTypeID") + "]<br/>");
		                System.out.println("OrderNo      = [" + json.GetKeyValue("OrderNo") + "]<br/>");
		                System.out.println("OrderDate      = [" + json.GetKeyValue("OrderDate") + "]<br/>");
		                System.out.println("OrderTime      = [" + json.GetKeyValue("OrderTime") + "]<br/>");
		                System.out.println("OrderAmount      = [" + json.GetKeyValue("OrderAmount") + "]<br/>");
		                System.out.println("Status      = [" + json.GetKeyValue("Status") + "]<br/>");
		                System.out.println("OrderDesc      = [" + json.GetKeyValue("OrderDesc") + "]<br/>");
		                System.out.println("OrderURL      = [" + json.GetKeyValue("OrderURL") + "]<br/>");
		                System.out.println("PaymentLinkType      = [" + json.GetKeyValue("PaymentLinkType") + "]<br/>");
		                System.out.println("AcctNo      = [" + json.GetKeyValue("AcctNo") + "]<br/>");
		                System.out.println("CommodityType      = [" + json.GetKeyValue("CommodityType") + "]<br/>");
		                System.out.println("ReceiverAddress      = [" + json.GetKeyValue("ReceiverAddress") + "]<br/>");
		                System.out.println("BuyIP      = [" + json.GetKeyValue("BuyIP") + "]<br/>");
		                System.out.println("iRspRef      = [" + json.GetKeyValue("iRspRef") + "]<br/>");
		                //System.out.println("HostTime      = [" + json.GetKeyValue("HostTime") + "]<br/>");
		                //System.out.println("HostDate      = [" + json.GetKeyValue("HostDate") + "]<br/>");
		                System.out.println("ReceiveAccount      = [" + json.GetKeyValue("ReceiveAccount")+ "]<br/>");
//		                System.out.println("ReceiveAccName      = [" + new String(json.GetKeyValue("ReceiveAccName").getBytes("iso-8859-1"), "utf-8") + "]<br/>");
		                System.out.println("MerchantRemarks      = [" + json.GetKeyValue("MerchantRemarks") + "]<br/>");
		            }            
		        }
		    }
		}else{
		    //6、商户订单查询失败
		    System.out.println("商户订单查询失败:ReturnCode   = [" + ReturnCode + "]<br/>");
		    System.out.println("商户订单查询失败:ErrorMessage = [" + ErrorMessage + "]<br/>");
		}
		return jsonObject;
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		ABCQuery query = new ABCQuery();
		System.out.println(query.query("49483046", "103882630000083"));
	}
}
